<script setup>
import { h, ref } from 'vue'
import { ElMessage, ElDivider } from 'element-plus'
import { Message, Lock, User,Phone } from '@element-plus/icons-vue'
import {register} from '@/api/user'
import {useRouter} from 'vue-router'
const router = useRouter()
const size = ref(2)
const spacer = h(ElDivider, { direction: 'vertical' })

// 注册表单数据
const registerForm = ref({
  username: '',
  phone:'',
  email: '',
  name:'',
  address:'',
  password: '',
  confirmPassword: ''
})

// 表单验证规则
const rules = {
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 2, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  name: [
    { required: true, message: '请输入真实姓名', trigger: 'blur' },
    { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
  ],
  address: [
    { required: true, message: '请输入生源地址', trigger: 'blur' },
    { min: 4, max: 20, message: '长度在 4 到 20 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== registerForm.value.password) {
          callback(new Error('两次输入密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

// 加载状态
const loading = ref(false)



// 注册方法
const handleRegister = async() => {
      loading.value = true
      // 这里模拟注册请求
      let result = await register(registerForm.value)  
      loading.value = false
      
      // 倒计时跳转登录页
      let countdown = 3
      ElMessage.success(`注册成功，${countdown}秒后自动跳转到登录页面`)
      const timer = setInterval(() => {
        countdown--
        if (countdown > 0) {
          ElMessage.success(`注册成功，${countdown}秒后自动跳转到登录页面`)
        } else {
          clearInterval(timer)
          router.push('/login')
        }
      }, 1000)
}
</script>

<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 左侧图片 -->
      <div class="login-image">
        <img src="@/assets/register.png" alt="智慧商城">
      </div>
      
      <!-- 右侧注册表单 -->
      <div class="login-form">
        <h2 class="login-title">智慧宿舍管理</h2>
        <p class="login-subtitle">欢迎注册，请填写以下信息</p>
        
        <el-form
          ref="registerFormRef"
          :model="registerForm"
          :rules="rules"
          label-position="top"
          class="login-form-content"
          @keyup.enter="handleRegister"
        >
          <el-form-item prop="username">
            <el-input
              v-model="registerForm.username"
              placeholder="请输入用户名"
              :size="'large'"
            >
              <template #prefix>
                <el-icon><User /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="phone">
            <el-input
              v-model="registerForm.phone"
              placeholder="请输入手机号"
              :size="'large'"
            >
              <template #prefix>
                <el-icon><Phone /></el-icon>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item prop="email">
            <el-input
              v-model="registerForm.email"
              placeholder="请输入邮箱"
              :size="'large'"
            >
              <template #prefix>
                <el-icon><Message /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="name">
            <el-input
              v-model="registerForm.name"
              placeholder="请输入真实姓名"
              :size="'large'"
            >
              <template #prefix>
                <el-icon><Message /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="address">
            <el-input
              v-model="registerForm.address"
              placeholder="请输入生源地址"
              :size="'large'"
            >
              <template #prefix>
                <el-icon><Message /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="registerForm.password"
              type="password"
              placeholder="请输入密码"
              show-password
              :size="'large'"
            >
              <template #prefix>
                <el-icon><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item prop="confirmPassword">
            <el-input
              v-model="registerForm.confirmPassword"
              type="password"
              placeholder="请确认密码"
              show-password
              :size="'large'"
            >
              <template #prefix>
                <el-icon><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          
          <div class="login-options">
            <div class="login-links">
              <el-link type="primary" :underline="false" href="/login">已有账号？去登录</el-link>
            </div>
          </div>
          
          <el-button
            type="primary"
            :loading="loading"
            class="login-button"
            @click="handleRegister"
          >
            注册
          </el-button>
        </el-form>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
  
  .login-box {
    display: flex;
    width: 1100px;
    height: 800px;
    background-color: #fff;
    border-radius: 16px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
    overflow: hidden;
    
    .login-image {
      flex: 1.3;
      background-color: #f0f7ff;
      display: flex;
      justify-content: center;
      align-items: center;
      padding: 40px;
      
      img {
        max-width: 100%;
        max-height: 100%;
      }
    }
    
    .login-form {
      flex: 0.7;
      padding: 48px;
      display: flex;
      flex-direction: column;
      align-items: center;
      
      .login-form-content {
        width: 100%;
        max-width: 500px;
      }
      
      .login-title {
        font-size: 32px;
        font-weight: 700;
        color: #1a1a1a;
        margin-bottom: 16px;
        text-align: center;
      }
      
      .login-subtitle {
        font-size: 16px;
        color: #666666;
        margin-bottom: 40px;
        text-align: center;
      }

      .verification-code-input {
        display: flex;
        gap: 10px;

        .el-input {
          flex: 1;
        }

        .el-button {
          width: 120px;
        }
      }
      
      .login-options {
        display: flex;
        justify-content: left;
        align-items: center;
        margin-bottom: 25px;
        
        .login-links {
          display: flex;
          align-items: left;
        }
      }
      
      .login-button {
        width: 100%;
        height: 46px;
        font-size: 16px;
        font-weight: 600;
        letter-spacing: 1px;
        border-radius: 8px;
        margin-top: 8px;
      }
    }
  }
}

@media screen and (max-width: 768px) {
  .login-box {
    width: 92%;
    max-width: 480px;
    flex-direction: column;
    height: auto;
    margin: 20px;
    
    .login-image {
      display: none;
    }
  }
}
</style>